// 引入Sequelize模块
const Sequelize = require('sequelize');

// 引入数据库实例
const db = require('../db');
const user = require('./user');

const artPainting = db.define("artPainting", {
    // 主键
    objectId: {
        type: Sequelize.STRING(20),
        allowNull: false,
        unique: true,
        primaryKey: true
    },

    userId: {
        type: Sequelize.STRING(20),
        comment: "创建人"
    },

    nameZh: {
        type: Sequelize.STRING(50),
        comment: "中文名"
    },

    extraNameZh: {
        type: Sequelize.STRING(50),
        comment: "别名"
    },

    nameEn: {
        type: Sequelize.STRING(100),
        comment: "英文名"
    },

    author: {
        type: Sequelize.STRING(30),
        comment: "作者"
    },

    year: {
        type: Sequelize.STRING(10),
        comment: "创作日期"
    },

    size: {
        type: Sequelize.STRING(30),
        comment: "尺寸"
    },

    class: {
        type: Sequelize.STRING(30),
        comment: "类别"
    },

    material: {
        type: Sequelize.STRING(100),
        comment: "材质"
    },

    number: {
        type: Sequelize.STRING,
        comment: "编号"
    },

    desc: {
        type: Sequelize.TEXT,
        comment: "主要描述"
    },

    type: {
        type: Sequelize.STRING(20),
        comment: "画作分类"
    },

    extraDesc: {
        type: Sequelize.TEXT,
        comment: "更多描述"
    },

    price: {
        type: Sequelize.INTEGER,
        comment: "价格"
    },

    downLoadNum: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        comment: "下载次数"
    },

    imageUrl: {
        type: Sequelize.STRING,
        comment: "图片地址",
        defaultValue: "",
        get() {
            const images = this.getDataValue('images');
            const imageUrl = this.getDataValue('imageUrl');
            if (imageUrl) {
                return imageUrl
            } else if (images && images.length > 0) {
                return images[0]
            } else {
                return ""
            }
        }
    },

    images: {
        type: Sequelize.JSON,
        comment: "图片地址",
        defaultValue: [],
        get() {
            const images = this.getDataValue('images');
            const imageUrl = this.getDataValue('imageUrl');
            if (imageUrl) {
                return [imageUrl]
            } else if (images && images.length > 0) {
                return images
            } else {
                return []
            }
        }
    },

    dominantColor: {
        type: Sequelize.STRING(7),
        comment: "主色调1",
        defaultValue: null,
        allowNull: true
    },

    dominantColor2: {
        type: Sequelize.STRING(7),
        comment: "主色调2",
        defaultValue: null,
        allowNull: true
    },

    isNice: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
        comment: "是否是精选内容"
    },

    createdAt: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('createdAt');
            return dateFormat(rawValue, 'yyyy-mm-dd HH:MM:ss');
        }
    },

    updatedAt: {
        type: Sequelize.DATE,
        get() {
            const rawValue = this.getDataValue('updatedAt');
            return dateFormat(rawValue, 'yyyy-mm-dd HH:MM:ss');
        }
    },
}, {
    underscored: false,
    tableName: "artPainting",
});

user.user.hasMany(artPainting, {
    foreignKey: 'userId'
});
artPainting.belongsTo(user.user);

/*(async () => {
    artPainting.sync({
        alter: true
    });

    console.log("世界名画同步成功")
})();*/

module.exports = artPainting;
